package com.tt.vtg.scenario.redis;

import com.tt.vtg.scenario.util.ValueUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public enum JedisTemplate {
    INSTANCE(new JedisPool(ValueUtils.get("redis.host"),
                            Integer.valueOf(ValueUtils.get("redis.port")).intValue()));
    private JedisPool pool;
    JedisTemplate(JedisPool pool){
        this.pool = pool;
    }
    public static Jedis jedis(){
        return INSTANCE.pool.getResource();
    }

    public static <T> T execute(JedisCallback<T> callback){
        Jedis jedis = jedis();
        try{
            return callback.action(jedis);
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            jedis.close();
        }
        return null;
    }

}
